/*
 * Created by JFormDesigner on Thu Jun 01 11:56:37 CST 2023
 */

package gui.register;

import dao.UserDao;
import entity.User;
import gui.admin.AdminMainFrame;
import gui.user.UserMainFrame;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 * @author Liubuzhu
 */
public class LoginFrame extends JFrame {
    public LoginFrame() {
        initComponents();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        passwordField1 = new JPasswordField();
        button1 = new JButton();
        registerBtn = new JButton();
        passwordBtn = new JButton();

        //======== this ========
        setPreferredSize(new Dimension(590, 450));
        setTitle("\u767b\u5f55\u9875\u9762");
        setVisible(true);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u8d26\u53f7");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label1);
        label1.setBounds(105, 45, 100, 45);

        //---- label2 ----
        label2.setText("\u5bc6\u7801");
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label2);
        label2.setBounds(105, 210, 100, 45);
        contentPane.add(textField1);
        textField1.setBounds(220, 50, 230, 35);
        contentPane.add(passwordField1);
        passwordField1.setBounds(220, 215, 230, 35);

        registerBtn.setText("注册用户");
        contentPane.add(registerBtn);
        registerBtn.setBounds(220,270,90,20);
        registerBtn.setOpaque(false);

        registerBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                dispose();
                RegisterFrame registerFrame = new RegisterFrame();
            }
        });



        //---- button1 ----
        button1.setText("\u767b\u5f55");
        button1.setIcon(new ImageIcon("./image/login/登录按钮.png"));
        contentPane.add(button1);
        button1.setBounds(425, 325, 105, 45);

        userRadionButton.setBounds(320,325,75,36);//设置用户选项
        userRadionButton.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        userRadionButton.setOpaque(false);
        this.getContentPane().add(userRadionButton);

        adminRadionButton.setBounds(220,325,75,36);//设置管理员选项
        adminRadionButton.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        adminRadionButton.setOpaque(false);
        this.getContentPane().add(adminRadionButton);

        buttonGroup.add(adminRadionButton);//设置选项唯一
        buttonGroup.add(userRadionButton);//设置选项唯一

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String name = textField1.getText();//获取用户名输入框输入的内容
                char[] passArr = passwordField1.getPassword();//获取密码输入框输入的内容
                String pass = new String(passArr);


                if(adminRadionButton.isSelected()){
                    //如果管理员按钮被选中
                    if (name.equals("admin") && pass.equals("123456")){
                        JOptionPane.showMessageDialog(null,"登陆成功");//显示登陆成功信息
                        dispose();
                        try {
                            AdminMainFrame adminMainFrame = new AdminMainFrame();
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }else{
                        JOptionPane.showMessageDialog(null,"用户名或密码错误");//显示登陆失败信息
                        textField1.setText("");//初始化用户名输入框
                        passwordField1.setText("");//初始化密码输入框
                    }
                } else if (userRadionButton.isSelected()){
                    //如果用户按钮被选中
                    UserDao userDao = new UserDao();
                    User user = null;
                    try {
                        user = userDao.login(name);
                        if (user == null){
                            JOptionPane.showMessageDialog(null,"未查找到该用户");//显示登陆失败信息
                            textField1.setText("");//初始化用户名输入框
                            passwordField1.setText("");//初始化密码输入框
                        }else if (!pass.equals(user.getUserPassword())){
                            JOptionPane.showMessageDialog(null,"密码输入错误");//显示登陆失败信息
                            passwordField1.setText("");//初始化密码输入框
                        } else {
                            JOptionPane.showMessageDialog(null,"登陆成功");//显示登陆成功信息
                            String userId = user.getUserId();//获取用户编号
                            dispose();
                            UserMainFrame userMainFrame = new UserMainFrame(userId);
                        }
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                } else {
                    JOptionPane.showMessageDialog(null,"您还未选择身份");//显示信息
                }
            }
        });

        //添加背景图片
        JLabel background = new JLabel(new ImageIcon("./image/login/Login.jpg"));
        background.setBounds(0, 0, 590, 420);
        this.getContentPane().add(background);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JPasswordField passwordField1;
    private JButton button1;
    private JButton registerBtn;
    private JButton passwordBtn;
    private JRadioButton adminRadionButton = new JRadioButton("管理员");//管理员选项
    private JRadioButton userRadionButton = new JRadioButton("用户");//用户选项
    private ButtonGroup buttonGroup = new ButtonGroup();//选项唯一
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on

    public static void main(String[] args) {
        LoginFrame loginFrame = new LoginFrame();
    }
}
